package com.huaao.micropolice.root.controllers.system;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.huaao.micropolice.common.util.JsonUtil;
import com.huaao.micropolice.core.model.system.Jobs;
import com.huaao.micropolice.core.service.system.JobsService;
import com.huaao.micropolice.root.controllers.BaseController;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("system/jobs")
public class JobsController extends BaseController {
	@Autowired
	private JobsService jobsService;
	private static JsonConfig jsonConfig = new JsonConfig();
	static{
		jsonConfig.setExcludes(new String[]{"dept","roles"});
	}
 
	/**
	 * 展示地区页面
	 * 
	 * @return
	 */
	@RequestMapping("index.do")
	public String index() {
		return "/system/jobs/index";
	}

	/**
	 * 表格JSON数据
	 * 
	 * @return
	 */
	@RequestMapping(value="findJobByDept.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String findJobByDept(String deptId) {
		List<Jobs> list = jobsService.findByDeptId(deptId);
		String result = JSONArray.fromObject(list,jsonConfig).toString();
		return result;
	}

	/**
	 * 添加或更新区域信息
	 * 
	 * @return
	 */
	@RequestMapping("save.do")
	public ResponseEntity<String> save(Jobs item) {
		if(StringUtils.isEmpty(item.getId())){
			jobsService.save(item);
		}else{
			jobsService.update(item);
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(successMap()));
	}
	
	/**
	 * 添加或更新区域信息
	 * 
	 * @return
	 */
	@RequestMapping("del.do")
	public ResponseEntity<String> del(String id) {
		try {
			jobsService.del(id);
			return super.ajaxModel(JSON, JsonUtil.toJson(successMap()));
		} catch (Exception e) {
			return super.ajaxModel(JSON, JsonUtil.toJson(failMap(e.getMessage())));
		}
		
	}
	
	@RequestMapping("add.do")
	public String add(String parentId) {
		return "/system/jobs/jobsinfo";
	}
	
	
	@RequestMapping("update.do")
	public String update(String id) {
		return "/system/jobs/jobsinfo";
	}
	
	@RequestMapping("dels.do")
	public ResponseEntity<String> dels(@RequestParam(value = "ids[]") String[] ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			for(String id: ids){
				jobsService.del(id);
			}
			map = successMap();
		} catch (Exception e) {
			map = failMap(e.getMessage());
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}
}

